﻿/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using MyPhotoIndex.Interfaces.Plugins;
using MyPhotoIndex.Utilities;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Interfaces;

namespace MyPhotoIndex.MainMDI
{
    public delegate MyPhotoIndexThumbnailViewer GetActiveViewerDelegate();

    public class PluginsHandler
    {
        private static GetActiveViewerDelegate m_GetActiveViewerDelegate;

        public static void BuildPluginMenus(ToolStripMenuItem pluginsMenu, GetActiveViewerDelegate getActiveViewer)
        {
            m_GetActiveViewerDelegate = getActiveViewer;

            ReadOnlyCollection<IUtilityPlugin> plugins = Plugins.Manager.GetPlugins<IUtilityPlugin>();
            if (plugins == null || 
                plugins.Count == 0)
            {
                return;
            }

            foreach (IUtilityPlugin plugin in plugins)
            {
                try
                {
                    List<IPluginMenuInfo> pluginMenuInfoList = new List<IPluginMenuInfo>();
                    plugin.RegisterMenues(pluginMenuInfoList);
                    foreach (IPluginMenuInfo menuInfo in pluginMenuInfoList)
                    {
                        ToolStripItem newItem = pluginsMenu.DropDownItems.Add(menuInfo.Name);
                        newItem.Tag = menuInfo;
                        if (menuInfo.Icon != null)
                        {
                            newItem.Image = menuInfo.Icon;
                        }

                        newItem.Click += new EventHandler(pluginMenuItem_Click);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log("Falied to load menu for plugin - " + plugin.Name, ex);
                }
            }

            pluginsMenu.DropDownOpening += new EventHandler(pluginsMenu_DropDownOpening);

        }

        static void pluginsMenu_DropDownOpening(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;

            ReadOnlyCollection<int> selectedImages = null;
            if (m_GetActiveViewerDelegate != null)
            {
                MyPhotoIndexThumbnailViewer viewer = m_GetActiveViewerDelegate();
                if (viewer != null)
                {
                    selectedImages = viewer.SelectedImages;
                }
            }

            foreach (ToolStripDropDownItem menuItem in menu.DropDownItems)
            {
                try
                {
                    IPluginMenuInfo pluginMenuInfo = (IPluginMenuInfo)menuItem.Tag;
                    if (pluginMenuInfo != null)
                    {
                        bool enabled = true;
                        pluginMenuInfo.QueryMenuState(selectedImages, out enabled);
                        menuItem.Enabled = enabled;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
        }

        private static void pluginMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripItem menuItem = (ToolStripItem)sender;
                IPluginMenuInfo pluginMenuInfo = (IPluginMenuInfo)menuItem.Tag;

                ReadOnlyCollection<int> selectedImages = null;
                if (m_GetActiveViewerDelegate != null)
                {
                    MyPhotoIndexThumbnailViewer viewer = m_GetActiveViewerDelegate();
                    if (viewer != null)
                    {
                        selectedImages = viewer.SelectedImages;
                    }
                }

                pluginMenuInfo.PerformClick(selectedImages);
            }
            catch (Exception ex)
            {
                ApplicationAlert.Show(ex.ToString(), "Error in plugin", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Logger.Log(ex);
            }
        }

        internal static void InvokeSelectionChanged(ReadOnlyCollection<int> selectedImages, SelectedImageInfo lastSelected)
        {
            ReadOnlyCollection<IUtilityPlugin> plugins = Plugins.Manager.GetPlugins<IUtilityPlugin>();
            if (plugins == null ||
                plugins.Count == 0)
            {
                return;
            }

            foreach (IUtilityPlugin plugin in plugins)
            {
                try
                {
                    plugin.ImageSelectionChanged(selectedImages, lastSelected);
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
        }
    }
}
